package com.xiaoxi.dragsortlistview.countdown;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;

import com.xiaoxi.dragsortlistview.R;
import com.xiaoxi.dragsortlistview.countdown.bean.CountBean;
import com.xiaoxi.dragsortlistview.countdown.bean.adapter.CountdownAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class CountdownDemo1Activity extends AppCompatActivity {
    private ListView listView;
    private CountdownAdapter mAdapter;
    private List<CountBean> mBeanList;
    private Timer timer = null;
    private void initDate() {

        for (int i = 0; i < 10; i++) {
            CountBean bean = new CountBean();
            bean.setButtonContent("item" + i);
            bean.setEndTime("2016-11-05 00:00:00");
            mBeanList.add(bean);
        }
        for (int i = 0; i < 10; i++) {
            CountBean bean = new CountBean();
            bean.setButtonContent("item" + i);
            bean.setEndTime("2016-11-04 00:00:00");
            mBeanList.add(bean);
        }

        for (int i = 0; i < 50; i++) {
            CountBean bean = new CountBean();
            bean.setButtonContent("item" + i);
            bean.setEndTime("2016-11-07 00:00:00");
            mBeanList.add(bean);
        }
        for (int i = 0; i < 10; i++) {
            CountBean bean = new CountBean();
            bean.setButtonContent("item" + i);
            bean.setEndTime("2016-11-06 00:00:00");
            mBeanList.add(bean);
        }
    }

    private void dateFresh(){
        if (timer == null) {
            timer =  new Timer();
        }
        timer.scheduleAtFixedRate(new MyTask(), 1000, 1000);
    }
    class MyTask extends TimerTask {
        @Override
        public void run() {
            handler.sendEmptyMessage(1);
        }
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int firstVP = listView.getFirstVisiblePosition();
            int childCount = listView.getChildCount();
//            LogUtil.e("  first = "+ firstVP + " count = "+childCount);
            for (int i = 1; i <childCount; i++) {
                View view = listView.getChildAt(i);
                if(firstVP+i-1 >= mBeanList.size()){
                    continue;
                }
                CountBean plan = mBeanList.get(firstVP+i-1);//firstVP+i
                mAdapter.updateTime(view, plan);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_countdown_demo1);
        listView = (ListView) findViewById(R.id.lv_count_list);
        mBeanList = new ArrayList<>();
        initDate();
        mAdapter = new CountdownAdapter(CountdownDemo1Activity.this, mBeanList);
        listView.setAdapter(mAdapter);
        dateFresh();
        mAdapter.notifyDataSetChanged();
    }
}
